<script lang="ts" setup name="XtxCity">
import { ref, watch } from "vue";
import { onClickOutside } from "@vueuse/core";
import { AreaList, CityResult } from "@/types/goods";
import axios from "axios";

defineProps<{
  city?: string;
}>();

const emit = defineEmits<{
  (e: "changeCity", value: CityResult): void;
}>();

const flag = ref(false);
const changeFlag = () => {
  console.log(flag.value);
  flag.value = !flag.value;
};
const target = ref(null);
onClickOutside(target, () => {
  flag.value = false;
});
const cityList = ref<AreaList[]>();
const defaCityList = ref<AreaList[]>();
const getCityList = async () => {
  const res = await axios.get<AreaList[]>(
    "https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json"
  );
  cityList.value = res.data;
  defaCityList.value = res.data;
};
getCityList();
const changeResult = ref({
  provinceCode: "",
  provinceName: "",
  cityCode: "",
  cityName: "",
  countyCode: "",
  countyName: "",
});

const selectCity = (city: AreaList) => {
  if (city.level === 0) {
    // 省
    changeResult.value.provinceName = city.name;
    changeResult.value.provinceCode = city.code;
    cityList.value = city.areaList;
  }
  if (city.level === 1) {
    // 市
    changeResult.value.cityName = city.name;
    changeResult.value.cityCode = city.code;
    cityList.value = city.areaList;
  }
  if (city.level === 2) {
    // 县（区）
    changeResult.value.countyName = city.name;
    changeResult.value.countyCode = city.code;
    // 关闭弹窗
    flag.value = false;

    emit("changeCity", changeResult.value);
  }
};
// #3 监听关闭弹窗的处理，恢复数据
watch(flag, (value) => {
  // 当关闭 active 的时候，需要回复数据
  if (!value) {
    cityList.value = defaCityList.value;
  }
});
</script>
<template>
  <div class="xtx-city" ref="target">
    <div class="select">
      <span class="value" v-if="city" @click="changeFlag">{{ city }}</span>
      <span class="placeholder" @click="changeFlag" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="flag">
      <span
        class="ellipsis"
        @click="selectCity(item)"
        v-for="item in cityList"
        :key="item.code"
        >{{ item.name }}</span
      >
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
